package com.wuzuqing.android.eightdiagrams.term;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public  class LunarUtil extends LunarConstant {

        public static int getTimeZhiIndex(String hm) {
            if (hm == null || hm.isEmpty()) {
                return 0;
            }
            if (hm.length() > 5) {
                hm = hm.substring(0, 5);
            }
            int hour = Integer.parseInt(hm.split(":")[0]);
            if (hour == 0 || hour == 23) {
                return 0;
            }
            return ((hour+1) / 2);
//           int x = 1;
//           int offset = hour/2;
//
//            for (int i = 1; i < 22; i += 2) {
//                if (hm >= ((i < 10 ? "0" : "") + i + ":00") && hm <= ((i + 1 < 10 ? "0" : "") + (i + 1) + ":59")) {
//                    return x;
//                }
//                x++;
//            }
//            return 0;
        }

        public static String convertTime(String hm) {
            return ZHI[getTimeZhiIndex(hm) + 1];
        }

        public static int getJiaZiIndex(String ganZhi) {
            for (int i = 0, j = JIA_ZI.length; i < j; i++) {
                if (Objects.equals(JIA_ZI[i], ganZhi)) {
                    return i;
                }
            }
            return -1;
        }

        public static String hex(Integer n) {
            String hex = Integer.toHexString(n);
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            return hex.toUpperCase();
        }

        public static List<String> getDayYi(String monthGanZhi, String dayGanZhi) {
            List<String> l = new ArrayList<>();
            String day = hex(getJiaZiIndex(dayGanZhi));
            String month = hex(getJiaZiIndex(monthGanZhi));
            String right = DAY_YI_JI;
            int index = right.indexOf(day + "=");
            while (index > -1) {
                right = right.substring(index + 3);
                String left = right;
                if (left.indexOf("=") > -1) {
                    left = left.substring(0, left.indexOf("=") - 2);
                }
                boolean matched = false;
                String months = left.substring(0, left.indexOf(":"));
                int i, j;
                String m;
                for (i = 0,
                             j = months.length(); i < j; i += 2) {
                    m = months.substring(i, 2);
                    if (m == month) {
                        matched = true;
                        break;
                    }
                }
                if (matched) {
                    String ys = left.substring(left.indexOf(":") + 1);
                    ys = ys.substring(0, ys.indexOf(","));
                    for (i = 0,
                                 j = ys.length(); i < j; i += 2) {
                        m = ys.substring(i, 2);
                        l.add(YI_JI[parseInt(m, 16)]);
                    }
                    break;
                }
                index = right.indexOf(day + "=");
            }
            if (l.size() < 1) {
                l.add("无");
            }
            return l;
        }

        public static List<String> getDayJi(String monthGanZhi, String dayGanZhi) {
            List<String> l = new ArrayList<>();
            String day = hex(getJiaZiIndex(dayGanZhi));
            String month = hex(getJiaZiIndex(monthGanZhi));
            String right = DAY_YI_JI;
            int index = right.indexOf(day + "=");
            while (index > -1) {
                right = right.substring(index + 3);
                String left = right;
                if (left.indexOf("=") > -1) {
                    left = left.substring(0, left.indexOf("=") - 2);
                }
                boolean matched = false;
                String months = left.substring(0, left.indexOf(":"));
                String m;
                int i, j;
                for (i = 0,
                             j = months.length(); i < j; i += 2) {
                    m = months.substring(i, 2);
                    if (m == month) {
                        matched = true;
                        break;
                    }
                }
                if (matched) {
                    String js = left.substring(left.indexOf(",") + 1);
                    for (i = 0,
                                 j = js.length(); i < j; i += 2) {
                        m = js.substring(i, 2);
                        l.add(YI_JI[parseInt(m, 16)]);
                    }
                    break;
                }
                index = right.indexOf(day + "=");
            }
            if (l.size() < 1) {
                l.add("无");
            }
            return l;
        }

        public static List<String> getDayJiShen(int lunarMonth, String dayGanZhi) {
            List<String> l = new ArrayList<>();
            String day = hex(getJiaZiIndex(dayGanZhi));
            String month = hex(Math.abs(lunarMonth));
            int index = DAY_SHEN_SHA.indexOf(month + day + "=");
            if (index > -1) {
                String left = DAY_SHEN_SHA.substring(index + 4);
                if (left.indexOf("=") > -1) {
                    left = left.substring(0, left.indexOf("=") - 3);
                }
                String js = left.substring(0, left.indexOf(","));
                for (int i = 0, j = js.length(); i < j; i += 2) {
                    String m = js.substring(i, 2);
                    l.add(SHEN_SHA[parseInt(m, 16)]);
                }
            }
            if (l.size() < 1) {
                l.add("无");
            }
            return l;
        }

        private static int parseInt(String m, int radix) {
            return Integer.parseInt(m, radix);
        }

        public static List<String> getDayXiongSha(int lunarMonth, String dayGanZhi) {
            List<String> l = new ArrayList<>();
            String day = hex(getJiaZiIndex(dayGanZhi));
            String month = hex(Math.abs(lunarMonth));
            int index = DAY_SHEN_SHA.indexOf(month + day + "=");
            if (index > -1) {
                String left = DAY_SHEN_SHA.substring(index + 4);
                if (left.indexOf("=") > -1) {
                    left = left.substring(0, left.indexOf("=") - 3);
                }
                String xs = left.substring(left.indexOf(",") + 1);
                for (int i = 0, j = xs.length(); i < j; i += 2) {
                    String m = xs.substring(i, 2);
                    l.add(SHEN_SHA[parseInt(m, 16)]);
                }
            }
            if (l.size() < 1) {
                l.add("无");
            }
            return l;
        }

        public static List<String> getTimeYi(String dayGanZhi, String timeGanZhi) {
            List<String> l = new ArrayList<>();
            String day = hex(getJiaZiIndex(dayGanZhi));
            String time = hex(getJiaZiIndex(timeGanZhi));
            int index = TIME_YI_JI.indexOf(day + time + "=");
            if (index > -1) {
                String left = TIME_YI_JI.substring(index + 5);
                if (left.indexOf("=") > -1) {
                    left = left.substring(0, left.indexOf("=") - 4);
                }
                String ys = left.substring(0, left.indexOf(","));
                for (int i = 0, j = ys.length(); i < j; i += 2) {
                    String m = ys.substring(i, 2);
                    l.add(YI_JI[parseInt(m, 16)]);
                }
            }
            if (l.size() < 1) {
                l.add("无");
            }
            return l;
        }

        public static List<String> getTimeJi(String dayGanZhi, String timeGanZhi) {
            List<String> l = new ArrayList<>();
            String day = hex(getJiaZiIndex(dayGanZhi));
            String time = hex(getJiaZiIndex(timeGanZhi));
            int index = TIME_YI_JI.indexOf(day + time + "=");
            if (index > -1) {
                String left = TIME_YI_JI.substring(index + 5);
                if (left.indexOf("=") > -1) {
                    left = left.substring(0, left.indexOf("=") - 4);
                }
                String js = left.substring(left.indexOf(",") + 1);
                for (int i = 0, j = js.length(); i < j; i += 2) {
                    String m = js.substring(i, 2);
                    l.add(YI_JI[parseInt(m, 16)]);
                }
            }
            if (l.size() < 1) {
                l.add("无");
            }
            return l;
        }

        public static int getXunIndex(String ganZhi) {
            String gan = ganZhi.substring(0, 1);
            String zhi = ganZhi.substring(1);
            int ganIndex = 0;
            int zhiIndex = 0;
            int i, j;
            for (i = 0,
                         j = GAN.length; i < j; i++) {
                if (GAN[i] == gan) {
                    ganIndex = i;
                    break;
                }
            }
            for (i = 0,  j = ZHI.length; i < j; i++) {
                if (ZHI[i] == zhi) {
                    zhiIndex = i;
                    break;
                }
            }
            int diff = ganIndex - zhiIndex;
            if (diff < 0) {
                diff += 12;
            }
            return diff / 2;
        }

        public static String getXun(String ganZhi) {
            return XUN[getXunIndex(ganZhi)];
        }

        public static String getXunKong(String ganZhi) {
            return XUN_KONG[getXunIndex(ganZhi)];
        }
    }

